<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys"%>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${tabTitle}</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div>
			<div id="pageInTab" class="pageInTab">
				<form id="ins_cfg_info_edit_form_snbanks">
					<div class="fieldset1 tabel_col clearFix">
						<div class="fieldBox col_50">
							<div class="label">机构名称</div>
							<div class="field">
								<span name="fundRasInsNm" id="fundRasInsNm">${fundRasInsNm}</span>
							</div>
						</div>
						<div class="fieldBox col_50">
							<div class="label">客户名称</div>
							<div class="field">
								<span >${cstNm}</span>
							</div>
						</div>
                        <div class="fieldBox col_50">
                            <div class="label">Ⅱ类账户</div>
                            <div class="field">
                                <span >${insAccNo}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_50">
                            <div class="label">证件有效起始日期</div>
                            <div class="field">
                                <input name="certValidSDt" id="certValidSDt" type="text" class="ui-datepicker" value="${certValidSDt}" validate="{required:true}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_50 col_fix_photo">
                            <div class="label">证件有效终止日期</div>
                            <div class="field">
                                <input name="certValidEDt" id="certValidEDt" type="text" class="ui-datepicker" value="${certValidEDt}" >
                                <div><input type="checkbox" name="checkOne" id="checkOne"  <c:if test="${certValidEDt=='2099-12-31'}">checked</c:if> value="长期" onchange="enduring(this)">长期</div>
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_50">
                            <div class="label">签发机关</div>
                            <div class="field">
                                <input name="lssueOffice" id="lssueOffice" type="text" class="ui-textbox" value="${lssueOffice}" >
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
						<div class="fieldBox col_50">
							<div class="label">开户银行</div>
							<div class="field">
                                <select name="bkCd" id="bkCd" value="${bkCd}" validate="{required:true}">
                                    <c:if test="${bkCd == null || bkCd == ''}">
                                        <option value="" selected="selected">--请选择--</option>
                                    </c:if>
                                    <c:if test="${bkCd != null && bkCd != ''}">
                                        <option value="">--请选择--</option>
                                    </c:if>
                                    <c:forEach items="${bankList}" var="item">
                                        <c:if test="${item.key == bkCd}">
                                            <option value="${item.key}" selected="selected">${item.value}</option>
                                        </c:if>
                                        <c:if test="${item.key != bkCd}">
                                            <option value="${item.key}">${item.value}</option>
                                        </c:if>
                                    </c:forEach>
                                </select>
								<span class="requiredSymbol">*</span>
							</div>
						</div>
						<div class="fieldBox col_50">
							<div class="label">银行账号</div>
							<div class="field">
								<input name="accNo" id="accNo" type="text" class="ui-textbox" value="${accNo}" validate="{number:true, maxlength:22, minlength:15}">
								<span class="requiredSymbol">*</span>
							</div>
						</div>
						<div class="fieldBox col_50">
							<div class="label">预留手机号</div>
							<div class="field">
								<input name="mp" id="mp" type="text" class="ui-textbox" value="${mp}" validate="{number:true, maxlength:11, minlength:11}">
								<span class="requiredSymbol">*</span>
							</div>
						</div>
                        <div class="fieldBox col_50">

                        </div>
                        <div class="fieldBox col_50">
                            <div class="label">住宅地址</div>
                            <div class="field">
                                <jsp:include page="../../system/sysRegion/region.jsp" flush="true">
                                    <jsp:param name="regionShowLvl"  value="2"/>
                                </jsp:include>
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_50">
                            <div class="label">住宅地址明细</div>
                            <div class="field">
                                <input name="postAddr" id="postAddr" type="text" class="ui-textbox" value="${postAddr}" validate="{maxlength:150}">
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                        <div class="fieldBox col_50">
                            <div class="label">开户结果</div>
                            <div class="field">
                                <span >${accResCd}</span>
                            </div>
                        </div>
                        <div class="fieldBox col_100">
                            <div class="label">开户失败原因</div>
                            <div class="field">
                                <span >${accResMsg}</span>
                            </div>
                        </div>

						<!-- hide data -->
						<div class="fieldBox col_100" style="display: none;">
							<div class="field">
								<input name="prjId" id="prjId" type="text" class="ui-textbox" value="${prjId}">
								<input name="cstId" id="cstId" type="text" class="ui-textbox" value="${cstId}">
                                <input name="insCode" id="insCode" type="text" class="ui-textbox" value="${insCode}">
                                <input name="cstNm" id="cstNm" type="text" class="ui-textbox" value="${cstNm}">
								<input name="cstAccInfoId" id="cstAccInfoId" type="text" class="ui-textbox" value="${cstAccInfoId}">
                                <input name="fundRasInsId" id=fundRasInsId type="text" class="ui-textbox" value="${fundRasInsId}">
                                <input name="signAgreementNo" id=signAgreementNo type="text" class="ui-textbox" value="${signAgreementNo}">
							</div>
						</div>
					</div>
	                <div id="saveInsCfgDiv" class="btnsBox">
                        <span id="saveInsCfg" class="sq-button sq-button-round" onclick="return saveInfo();">保存</span>
                	</div>
                    <div class="fieldset1 tabel_col clearFix" id="openAccountDiv">
                        <div class="fieldBox col_100_auto" >
                            <div class="label">电子账户开户</div>
                            <div class="field">
                                <input name="phoneCode" id="phoneCode" type="text" class="ui-textbox" value="">
                                <span class="requiredSymbol">*</span>
                                <div>
                                    <span id="sendVerifyCode" class= "sq-button sq-button-round" onclick="getPhoneCode(this)">获取验证码</span>
                                    <span id="openAccount" class= "sq-button sq-button-round" onclick="openAccount(this)">电子账户开户</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
			</div>
		</div>
	</div>

<script type="text/javascript">
var cstAccInfoId = '${cstAccInfoId}';
var certNo = '${certNo}';
var detailFlag = '${detailFlag}';
var certValidEDt;
var insAccNo = '${insAccNo}';
var accResCd = '${accResCd}';
$(function () {
	initData();
    enduring();
});

function initData() {
    $("#ins_cfg_info_edit_form_snbanks").ligerForm({
        validate: true
    });
	disabledInput();
    //设置省、市
    setRgnProvCdUpdataValue('${postProvince}');
    setRgnCityCdUpdataValue('${postCity}');
}

 function disabledInput() {
    //流程附件页面 和开户成功 开户中不 可以编辑
	if(detailFlag == "true" || "开户成功" == accResCd || "开户中" == accResCd) {
		liger.get("bkCd").setDisabled();
		liger.get("accNo").setDisabled();
		liger.get("mp").setDisabled();
		liger.get("certValidSDt").setDisabled();
		liger.get("certValidEDt").setDisabled();
		liger.get("lssueOffice").setDisabled();
        liger.get("rgnProvCd").setDisabled();
        liger.get("rgnCityCd").setDisabled();
        liger.get("postAddr").setDisabled();
		$("#saveInsCfgDiv").hide();
	}

	if (detailFlag != "true" && "开户失败" == accResCd) {
        $("#openAccountDiv").show();
    } else {
        $("#openAccountDiv").hide();
    }
}


/**
 * 校验时间格式
 */
function checkDateFormat(){
	var flag = 0;
	var params = {};
	params["certValidSDt"] = $("#certValidSDt").val();
    params["certValidEDt"] = $("#certValidEDt").val();
	$.ajax({
		type:'get',
		dataType:'json',
		async:false,
		url:'<%=path%>/customer/cstbscinfo/checkCertValid.do',
		data:params,
		success:function(data){
			//$.ligerDialog.SQalert({content: data.info,SQ_type: "success"});	
			if(data.success == 1){
				var oDate1 = new Date($("#certValidSDt").val());
                var oDate2 = new Date($("#certValidEDt").val());;
		        if(oDate1.getTime() < oDate2.getTime()){
					flag = 1;
		        }
		        else {
		        	UIUtils.alertError("证件有效起始日期 不能大于等于 证件有效终止日期");
		        }
			}else{
				UIUtils.alertError(data.info);
			}
		},
		error:function(data){
			UIUtils.alertError(data.info);
		}
	});
	return flag;
}

// 保存项目客户信息，后面弹出准入验证的资料确认页
function saveInfo() {
	var ligerForm = liger.get("ins_cfg_info_edit_form_snbanks");
    if (!ligerForm.valid()) {
        top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
        return false; //验证不通过，不执行以下代码
    }        

    //长期无需校验
    /*if (null == certValidEDt) {
        // 校验 身份证生效日 和 身份证到期日 数据格式
        if(checkDateFormat() != 1){
            return ;
        }
    }*/
    var data = ligerForm.getData();
    if(!$("#certValidSDt").val()){
        top.$.ligerDialog.SQalert({content: "请选择证件有效起始日期！", SQ_type: "error"});
        return false;
    }
    if(null == certValidEDt && !$("#certValidEDt").val()){
        top.$.ligerDialog.SQalert({content: "请选择证件有效终止日期！", SQ_type: "error"});
        return false;
    }
	if(!$("#lssueOffice").val()){
        top.$.ligerDialog.SQalert({content: "请填写签发机关！", SQ_type: "error"});
        return false;
    }
     if(!$("#bkCd").val()){
        top.$.ligerDialog.SQalert({content: "请选择开户银行！", SQ_type: "error"});
        return false;
    }
    if(!$("#accNo").val()){
        top.$.ligerDialog.SQalert({content: "请填写银行账号！", SQ_type: "error"});
        return false;
    }
    if(!$("#mp").val()){
        top.$.ligerDialog.SQalert({content: "请填写预留手机号码！", SQ_type: "error"});
        return false;
    }
    if(!getRgnProvCd()){
        top.$.ligerDialog.SQalert({content: "请选择住宅地址-省！", SQ_type: "error"});
        return false;
    }
    if(!getRgnCityCd()){
        top.$.ligerDialog.SQalert({content: "请选择住宅地址-市！", SQ_type: "error"});
        return false;
    }
    if(!$("#postAddr").val()){
        top.$.ligerDialog.SQalert({content: "请填写住宅详细地址！", SQ_type: "error"});
        return false;
    }
    // data["bkNm"] = liger.get("bkCd").selectedText;
    data["cstAccInfoId"] = cstAccInfoId;//项目id
    data["prjId"] = $("#prjId").val();//项目id
    data["cstId"] = $("#cstId").val();//客户id
    data["insCode"] = $("#insCode").val();//机构code
    data["certValidSDt"] = $("#certValidSDt").val();//身份证有效期
    if (null == certValidEDt) {
        data["certValidEDt"] = $("#certValidEDt").val();//身份证截止日期
    } else {
        data["certValidEDt"] = certValidEDt;
    }
    data["lssueOffice"] = $("#lssueOffice").val();//签发机关
    data["fundRasInsId"] = $("#fundRasInsId").val();//机构
    data["certNo"] = certNo;//身份证号
    var index=document.getElementById("bkCd").selectedIndex;//获取当前选择项的索引.
    data["bkNm"] = document.getElementById("bkCd").options[index].text;//获取当前选择项的文本.
    data["bkCd"] = liger.get("bkCd").selectedValue;//开户银行
    data["accNo"] =  $("#accNo").val();//银行账号
    data["cstNm"] = $("#cstNm").val();//客户名称
    data["mp"] = $("#mp").val(); //手机号
    data["postProvince"] = getRgnProvCd();
    data["postCity"] = getRgnCityCd();
    data["postAddr"] = $("#postAddr").val();
    var url = '<%=path%>/institution/snbanks/snBanksAddOrModifyPrjAccInfo.do';
    var wait_dialog;
    $.ajax({
        type: 'post',
        dataType: 'json',
        data: data,
        url: url,
        beforeSend: function () {
            wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
        },
        success: function (msg) {
            wait_dialog.close();
            if (msg.success == 1) {
            	//setData(msg.data);//设置数据
            	//mainData = msg.data;
            	disabledInput();
            	//setData();
				UIUtils.alertSuccessHold1(msg.info);
            } else {
                UIUtils.alertErrorHold1(msg.info);
            }
        }
    });
}

//选择长期置灰时间控件
function enduring(checkbox) {
    if ($("#checkOne").is(':checked')){
        $("#certValidEDt").hide();
        certValidEDt = "2099-12-31";
    } else {
        $("#certValidEDt").show();
        certValidEDt = null;
    }
}

function getPhoneCode(obj) {
    var prjId = $("#prjId").val();
    var cstId = $("#cstId").val();
    $.ajax({
        url:"<%=path%>/institution/snbanks/snbanksSendVcode.do",
        data:{
            prjId:prjId,
            cstId:cstId,
            prjCstAccId:cstAccInfoId
        },
        dataType:"json",
        type:"get",
        async:false,
        cache:false,
        success: function (msg) {
            if (msg.success == 1) {
                top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                settime(obj, 60);
            } else {
                top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
            }
        },
        error:function () {
            UIUtils.alertError("验证码发送失败");
        }
    });
}
function openAccount(obj) {
    var phoneCode = $("#phoneCode").val();
    if(!phoneCode){
        top.$.ligerDialog.SQalert({content: "请填写验证码！", SQ_type: "error"});
        return false;
    }
    var prjId = $("#prjId").val();
    $.ajax({
        url:"<%=path%>/institution/snbanks/snbanksOpenAccount.do",
        data:{
            prjId:prjId,
            prjCstAccId:cstAccInfoId,
            phoneCode:phoneCode,
        },
        dataType:"json",
        type:"get",
        async:false,
        cache:false,
        success: function (msg) {
            if (msg.success == 1) {
                top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
            } else {
                top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
            }
        },
        error:function () {
            UIUtils.alertError("电子账户开户失败");
        }
    });
}
function settime(obj, countdown) { //发送验证码倒计时
    if (countdown == 0) {
        $(obj).removeClass("disabledBtn")
        $(obj).attr("style", "background: ");
        $(obj).attr("isSend", "0");
        $(obj).text("获取验证码");
        countdown = 60;
        return;
    } else {
        $(obj).addClass("disabledBtn")
        $(obj).attr("style", "background: gray;");
        $(obj).attr("isSend", "1");
        $(obj).text("重新发送(" + countdown + ")");
        countdown--;
    }
    setTimeout(function () {
        settime($(obj), countdown)
    }, 1000)
}


</script>

</body>
</html>